//
//  LoginCodeViewController.m
//  ZhuanZuBao
//
//  Created by bchy on 2020/8/31.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "LoginCodeViewController.h"
#import "CodeInputView.h"

@interface LoginCodeViewController ()<UITextFieldDelegate>
{
    NSInteger countNum;
}


@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@property (nonatomic, strong) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (nonatomic, strong) CodeInputView *checkVerifyCodeView;
@property (nonatomic, copy) NSString *code;
@end

@implementation LoginCodeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _phoneLabel.text = _phone;
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#71D191" andAlpha:1].CGColor, (__bridge id)[UIColor colorWithHexString:@"#06B970" andAlpha:1].CGColor];
    gradientLayer.locations = @[@0.3, @0.6];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = CGRectMake(0, 0, DEVICE_SCREEN_WIDTH-24, 54);
    [self.loginBtn.layer addSublayer:gradientLayer];

//    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.shadowOffset = CGSizeMake(2, 2);
    self.loginBtn.layer.shadowColor = THEMECOLOR.CGColor;
    self.loginBtn.layer.shadowOpacity = 0.5;
    
    countNum = 60;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
    self.timer = timer;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.checkVerifyCodeView = [[CodeInputView alloc]initWithFrame:CGRectMake(0, 210, DEVICE_SCREEN_WIDTH, 80) inputType:6 selectCodeBlock:^(NSString * code) {
            NSLog(@"code === %@",code);
            self.code = code;
        }];
        [self.view addSubview:self.checkVerifyCodeView];
    });
}

- (IBAction)sendCode:(id)sender {
    if (countNum != 0) {
        return;
    }
    [MBProgressHUD showMessag:@"" toView:self.view];
    NSDictionary *dict = @{
        @"type":@"1",
        @"phone": _phone
    };
    [NetWork sendGetUrl:@"user/get_code.php" withParams:dict success:^(id responseBody) {
        NSLog(@"%@", responseBody);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
        self.timer = timer;
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

-(void)refreshTime{
    countNum--;
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%lds",(long)countNum] forState:UIControlStateNormal];
    if (countNum == 0) {
        countNum = 60;
        self.codeBtn.userInteractionEnabled = YES;
        [self.codeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (IBAction)login:(id)sender {
    [self.view endEditing:YES];
    
    if (self.code.length < 6) {
        [MBProgressHUD showWarning:@"请输入验证码" toView:nil];
        return;
    }
    NSDictionary *dict = @{
        @"usr": _phone,
        @"code": self.code
    };
    [MBProgressHUD showMessag:@"" toView:self.view];
    [NetWork sendGetUrl:@"user/login_code.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@", responseBody);
        User *user = [User sharedUser];
        user = [User mj_objectWithKeyValues:responseBody];
        WZLSERIALIZE_ARCHIVE(user, @"User", userFilePath);
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)tap{
    [self.view endEditing:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
